package com.suoluo.zenadmin.service.news;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.multipart.MultipartFile;

import com.suoluo.zenadmin.common.CommonResp;
import com.suoluo.zenadmin.common.PageUtil;
import com.suoluo.zenadmin.common.StringUtil;
import com.suoluo.zenadmin.common.SystemType;
import com.suoluo.zenadmin.dao.news.NewsDAO;
import com.suoluo.zenadmin.oss.OSSService;
import com.suoluo.zenadmin.service.ImageException;
import com.suoluo.zenadmin.service.InfoService;
import com.suoluo.zenadmin.service.ServiceException;
import com.suoluo.zenadmin.vo.BaseInfoVO;
import com.suoluo.zenadmin.vo.ImageVO;
import com.suoluo.zenadmin.vo.news.News2VO;

public class NewsServiceImpl implements NewsService{

	private static final Log logger = LogFactory.getLog(NewsServiceImpl.class);
	@Autowired
	private NewsDAO newsDAO;
	
	@Resource(name="ossService")
	private OSSService ossService;
	@Resource(name="infoService")
	private InfoService infoService;
	
	@Override
	public List<News2VO> findSimplestNews(int curPage, int pageRows) {
		
		return newsDAO.findSimplestNews(curPage,pageRows);
	}

	@Override
	public void saveNews(News2VO newsVO) {
		try {
			newsVO.setViewnum(1);
			//保存新闻信息
			int newsId = newsDAO.saveNews(newsVO);
			//设置数据类型
			newsVO.setInfoType(SystemType.INFO_NEWS);
			newsVO.setNewsId(newsId);
			newsVO.setDefaultInfo(SystemType.BIT_TRUE);
			newsVO.setDataId(newsId);
			
			//保存基础信息
			infoService.saveBaseInfo(newsVO);
			//保存图片信息
			infoService.saveImage(newsVO);
			//更新图片Id
			newsDAO.updateNewsImage(newsId,newsVO.getImageId());
		} catch (ImageException e) {
			throw e;
		} catch (Exception e) {
			ossService.deleteImage(newsVO.getSource());
			throw new ServiceException(CommonResp.FAIL_CODE,"新闻保存失败！",e);
		}
		
	}

	@Override
	public News2VO findNewsById(int newsId) {
		News2VO news = newsDAO.findNewsById(newsId,SystemType.INFO_NEWS);
		news.setName(StringUtil.buildCompleteName(news.getZhname(), news.getEnname()));
		return news;
	}

	@Override
	public void updateNews(News2VO newsVO) {
		MultipartFile mfile = newsVO.getImagepart();
		try {
			//修改新闻信息
			newsDAO.updateNews(newsVO);
			//修改新闻基础信息
			infoService.updateBaseInfo(newsVO);
			//修改新闻图片
			if(mfile != null && !mfile.isEmpty())
			{
				infoService.updateImage(newsVO);
			}
			
		} catch (ImageException e) {
			throw e;
		} catch (Exception e) {
			if(mfile != null && !mfile.isEmpty())
			{
				ossService.deleteImage(newsVO.getSource());
			}
			
			throw new ServiceException(CommonResp.FAIL_CODE,"修改新闻失败！",e);
		}
		
	}

	@Override
	public void deleteNews(News2VO newsVO) {
		//判断新闻是否可以删除
		
		//删除数据
		newsDAO.deleteNews(newsVO.getNewsId());
		//删除图片
		infoService.deleteBaseInfo(newsVO);
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<Map> findListNewsType() {
		return newsDAO.findListNewsType();
	}

	@Override
	public int findNewsCount() {
		return newsDAO.findNewsCount();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<News2VO> findNewsWithoutContent(int curPage, int pageRows) {
		int start = PageUtil.calcStartRow(curPage, pageRows);
		List<News2VO> newses = newsDAO.findNewsWithoutContent(start, pageRows);
		for (News2VO news : newses){
			news.setName(StringUtil.buildCompleteName(news.getZhname(), news.getEnname()));
		}
		return newses;
	}

	@Override
	public List<News2VO> findSimilarNews(int newsId, int newsType, int pageSize) {
		List<News2VO> newsList =  newsDAO.findSimilarNews(newsId,newsType,pageSize);
		for (News2VO news : newsList){
			news.setName(StringUtil.buildCompleteName(news.getZhname(), news.getEnname()));
		}
		return newsList;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public List<BaseInfoVO> findSubNews(int newsId) {
//		int infoType = SystemType.INFO_NEWS;
		List<BaseInfoVO> subItems = infoService.findBaseInfos(newsId, SystemType.INFO_NEWS);
		Map<Integer,BaseInfoVO> subItemMap = new HashMap<Integer, BaseInfoVO>();
		int [] infoIds = new int[subItems.size()];
		int i = 0;
		for (BaseInfoVO sub : subItems){
			subItemMap.put(sub.getInfoId(), sub);
			infoIds[i++] = sub.getInfoId();
		}
		List<ImageVO> images = infoService.findImages(infoIds);
		for (ImageVO img : images){
			BaseInfoVO info = subItemMap.get(img.getInfoId());
			info.addImage(img);
		}
		return subItems;
//		return newsDAO.findSubNews(newsId,infoType);
		
	}

	@Override
	public News2VO findSimplestNewsById(int newsId) {
		News2VO news = newsDAO.findSimplestNewsById(newsId);
		news.setName(StringUtil.buildCompleteName(news.getZhname(), news.getEnname()));
		return news;
	}
	
	@Override
	public void saveNewsText(BaseInfoVO baseVO) {
		try{
			//设置数据类型
			baseVO.setInfoType(SystemType.INFO_NEWS);
			//设置属性为非默认
			baseVO.setDefaultInfo(SystemType.BIT_FALSE);
			//保存基础信息,获取infoId
			infoService.saveBaseInfo(baseVO);
			//保存图片信息
			infoService.saveImage(baseVO);
		}catch (ImageException e) {
			throw e;
		}
		catch (Exception e) {
			ossService.deleteImage(baseVO.getSource());
			throw new ServiceException(CommonResp.FAIL_CODE,"新闻多图保存失败！",e);
		}
	
	}

	@Override
	public void saveNewsImages(BaseInfoVO baseVO) {
		List<String> list = null;
		try{
			list = ossService.putImages(baseVO.getImageFiles());
			infoService.saveImage(baseVO.getInfoId(), list);
		}catch (ImageException e) {
			throw e;
		}
		catch (Exception e) {
			ossService.deleteImages(list);
			throw new ServiceException(CommonResp.FAIL_CODE,"保存新闻多图失败！",e);
		}
	}

//	@Override
//	public void deleteNewsImages(BaseInfoVO baseVO) {
//		
//		//删除图片信息
//		infoService.deleteImage(baseVO);
//		//删除基础信息
//		infoService.deleteBaseInfoById(baseVO.getInfoId());
//		
//		
//	}

//	@Override
//	public BaseInfoVO findNewsText(int infoId) {
//		List<BaseInfoVO> list = infoService.findBaseInfoByInfoId(infoId);
//		if(list != null && !list.isEmpty()) {
//			return list.get(0);
//		}
//		else
//		{
//			throw new ServiceException(CommonResp.FAIL_CODE,"没有查询到相关信息，不支持修改！");
//		}
//		
//	}
	

}
